//
//  XGL_PersonalReceivedGiftsViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/27.
//

import UIKit
import CollectionKit

class XGL_PersonalReceivedGiftsViewController: UIViewController,CustomNavgationBar{
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var collectionView: CollectionView!
    var giftDatas: ArrayDataSource<GiftData> = ArrayDataSource(data: [])
    
    var userId: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createXglView()
        self.setCollectionData()
        self.creatClearNavigationBar(title: "")
        self.getRecievedGiftsRequest()
    }
    

}

extension XGL_PersonalReceivedGiftsViewController{
    private func getRecievedGiftsRequest(){
        self.hudView.backgroundColor = .clear
        self.hudView.hudView.backgroundColor = .clear
        self.hudView.hudView.startAnimation()
        self.view.addSubview(self.hudView)
        self.hudView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(PanNavibarH)
        }
        PanRequestManager<XGL_RequestBaseResponse<GiftData>,XGL_CustomRequestError>().request(XGLPersonalRequestAPI(requestAPI: .RecievedGifts(userId: self.userId))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                if let datas = response.resultDatas {
                    self.giftDatas.data = datas
                    self.collectionView.isHidden = datas.isEmpty
                    self.collectionView.snp.updateConstraints { make in
                        make.height.equalTo(ceil(Double(datas.count) / 4.0) * 114.0)
                    }
                }
                break
            default:
                break
            }
        }
    }
}

extension XGL_PersonalReceivedGiftsViewController{
    private func createXglView(){
        let titleText = UILabel().then { make in
            make.text = "My Present"
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Semibold(fontSize: 32.0)
        }
        self.collectionView = CollectionView().then({ make in
            make.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
            make.layerCornerRadius = 12.0
            make.isHidden = true
        })
        self.view.addSubviews([titleText,self.collectionView])
        
        titleText.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(PanNavibarH + 15.0)
        }
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleText.snp.bottom).offset(16.0)
            make.left.equalTo(15.0)
            make.centerX.equalToSuperview()
            make.height.equalTo(0)
        }
    }
    
    func setCollectionData(){
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: GiftData, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let contentView = UIView()
            let iconView = UIImageView().then { make in
                make.loadImageWithUrl(data.imgUrl)
            }
            let coinLabel = UILabel().then({ make in
                make.textColor = UIColor.white
                make.text = "X\(data.num)"
                make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            })
            contentView.addSubviews([iconView,coinLabel])
            view.addSubview(contentView)
           
            coinLabel.snp.makeConstraints { make in
                make.top.equalTo(iconView.snp.bottom).offset(7.0)
                make.centerX.equalToSuperview()
            }
            iconView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
                make.width.height.equalTo(50.0)
            }
            contentView.snp.makeConstraints { make in
                make.bottom.equalTo(coinLabel.snp.bottom)
                make.centerX.width.equalToSuperview()
                make.centerY.equalToSuperview()
            }
        })
        let sizeSource = { (index: Int, data: GiftData, collectionSize: CGSize) -> CGSize in
            
            return CGSize(width: collectionSize.width / 4.0, height: 114.0)
        }
        let provider = BasicProvider(
            dataSource: self.giftDatas,
            viewSource: viewSource,
            sizeSource: sizeSource
        )
        self.collectionView.provider = provider
    }
}
